home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #1 / Amiga Plus CD - 2000 - No. 1.iso / Tools / Dev / Meshwriter / vlist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-03  |  5.6 KB  |  176 lines

  1. /*
  2. **      $VER: vlist.c 1.00 (20.03.1999)
  3. **
  4. **      Creation date : 20.03.1999
  5. **
  6. **      Description       :
  7. **         Standart saver module for meshwriter.library.
  8. **         Saves the mesh as list of vertices, only vertices.
  9. **
  10. **
  11. **      Written by Stephan Bielmann
  12. **
  13. */
  14.  
  15. /*************************** Includes *******************************/
  16.  
  17. /*
  18. ** Amiga includes
  19. */
  20. #include <dos/dos.h>
  21. #include <dos/stdio.h>
  22.  
  23. #include <clib/dos_protos.h>
  24. #include <clib/alib_stdio_protos.h>
  25.  
  26. /*
  27. ** Project includes
  28. */
  29. #include "meshwriter_private.h"
  30.  
  31. /**************************** Defines *******************************/
  32.  
  33. /*********************** Type definitions ***************************/
  34.  
  35. /********************** Private functions ***************************/
  36.  
  37. /********************** Public functions ****************************/
  38.  
  39. /********************************************************************\
  40. *                                                                    *
  41. * Name         : write3VLIST                                         *
  42. *                                                                    *
  43. * Description  : Writes an ASCII vertex list file.                   *
  44. *                                                                    *
  45. * Arguments    : vlistfile IN  : An already opened file stream.      *
  46. *                mesh      IN  : Pointer to the mesh.                *
  47. *                                                                    *
  48. * Return Value : RCNOERROR                                           *
  49. *                RCWRITEDATA                                         *
  50. *                                                                    *
  51. * Comment      :                                                     *
  52. *                                                                    *
  53. \********************************************************************/
  54. ULONG write3VLIST(BPTR vlistfile, TOCLMesh *mesh) {
  55.     TOCLVertex                    ver;
  56.  
  57.     /*
  58.     ** Write the vertex list
  59.     */              
  60.       if(mesh->polygons.firstNode!=NULL) {                 
  61.         pln=mesh->polygons.firstNode;
  62.         do {
  63.             /* Get the first point of the polygon, used to create all triangles with it */
  64.             if(pln->numberOfVertices>=3) {
  65.                 plv1=pln->firstNode;
  66.                 ver1=plv1->vertexNode->vertex;
  67.             
  68.                 plvi=plv1;
  69.                 do {    
  70.                     plv2=plvi->next;
  71.                     plv3=plv2->next;
  72.                 
  73.                     ver2=plv2->vertexNode->vertex;
  74.                     ver3=plv3->vertexNode->vertex;
  75.                 
  76.                     sprintf(buffer,"%g %g %g\n",ver1.x,ver1.y,ver1.z,
  77.                     if(FPuts(rawfile,buffer)!=DOSFALSE) return(RCWRITEDATA);
  78.                     
  79.                     plvi=plvi->next;
  80.                 } while(plv3->next!=NULL);    
  81.             }
  82.         
  83.             /* Get the next polygon */
  84.             pln=pln->next;            
  85.         } while(pln!=NULL);
  86.     }
  87.     
  88.     return(RCNOERROR);
  89. }
  90.  
  91. /********************************************************************\
  92. *                                                                    *
  93. * Name         : write3RAWB                                          *
  94. *                                                                    *
  95. * Description  : Writes a standart RAW binary file.                  *
  96. *                                                                    *
  97. * Arguments    : rawfile IN  : An already opened file stream.        *
  98. *                mesh    IN  : Pointer to the mesh.                  *
  99. *                                                                    *
  100. * Return Value : RCNOERROR                                           *
  101. *                RCWRITEDATA                                         *
  102. *                                                                    *
  103. * Comment      :                                                     *
  104. *                                                                    *
  105. \********************************************************************/
  106. ULONG write3RAWB(BPTR rawfile, TOCLMesh *mesh) {
  107.     TOCLPolygonNode            *pln=NULL;    
  108.     TOCLPolygonsVerticesNode    *plvi=NULL,*plv1=NULL,*plv2=NULL,*plv3=NULL;
  109.     TOCLVertex                    ver1,ver2,ver3;
  110.     RAWTriangle                tbuffer[Ci_BUFFERS]; // Ci_BUFFERS * sizeof(RAWTriangle)    triangle buffer
  111.     ULONG                        bufferstate;
  112.     
  113.     /*
  114.     ** Write the name of the mesh, including its '\0' at the end
  115.     */
  116.     if(FWrite(rawfile,mesh->name,stringlen(mesh->name)+1,1)!=1) return(RCWRITEDATA);
  117.     
  118.     /*
  119.     ** Write the polygons as triangles, must be convex polygons !
  120.     */              
  121.       if(mesh->polygons.firstNode!=NULL) {                 
  122.         bufferstate=0;
  123.     
  124.         pln=mesh->polygons.firstNode;
  125.         do {
  126.             /* Get the first point of the polygon, used to create all triangles with it */
  127.             if(pln->numberOfVertices>=3) {
  128.                 plv1=pln->firstNode;
  129.                 ver1=plv1->vertexNode->vertex;
  130.             
  131.                 plvi=plv1;
  132.                 do {    
  133.                     plv2=plvi->next;
  134.                     plv3=plv2->next;
  135.                 
  136.                     ver2=plv2->vertexNode->vertex;
  137.                     ver3=plv3->vertexNode->vertex;
  138.                     
  139.                     tbuffer[bufferstate].x1=ver1.x;
  140.                     tbuffer[bufferstate].y1=ver1.y;
  141.                     tbuffer[bufferstate].z1=ver1.z;
  142.  
  143.                     tbuffer[bufferstate].x2=ver2.x;
  144.                     tbuffer[bufferstate].y2=ver2.y;
  145.                     tbuffer[bufferstate].z2=ver2.z;
  146.  
  147.                     tbuffer[bufferstate].x3=ver3.x;
  148.                     tbuffer[bufferstate].y3=ver3.y;
  149.                     tbuffer[bufferstate].z3=ver3.z;
  150.  
  151.                     // check if the buffer is full and write and initialize it
  152.                     if (++bufferstate==Ci_BUFFERS) {
  153.                         if(FWrite(rawfile,&tbuffer,Ci_BUFFERS*sizeof(RAWTriangle),1)!=1) return(RCWRITEDATA);
  154.                         bufferstate=0;
  155.                     }
  156.  
  157.                     plvi=plvi->next;
  158.                 } while(plv3->next!=NULL);    
  159.             }
  160.         
  161.             /* Get the next polygon */
  162.             pln=pln->next;            
  163.         } while(pln!=NULL);
  164.  
  165.         // check if the buffer has still some elements and write them
  166.         if (bufferstate>0) {
  167.             if(FWrite(rawfile,&tbuffer,bufferstate*sizeof(RAWTriangle),1)!=1) return(RCWRITEDATA);
  168.             bufferstate=0;
  169.         }
  170.     }
  171.     
  172.     return(RCNOERROR);
  173. }
  174.  
  175. /************************* End of file ******************************/
  176.